CONTROLLING THE LED WITH MY MQTT APPLICATION
In this project, we are going to control the LED which is connected to the NodeMCU Board with MyMQTT application by connecting to the centralized Mosquitto Server (test.mosquitto.org). By sending value ‘1’ we can turn ON the LED and by sending value ‘0’ it will turn OFF the LED.
Synopsis

In this project, we are going to control the LED which is connected to the NodeMCU Board with MyMQTT application by connecting to the centralized Mosquitto Server (test.mosquitto.org). By sending value ‘1’ we can turn ON the LED and by sending value ‘0’ it will turn OFF the LED.

Description

NodeMCU and MyMQTT mobile application connected to the same Mosquitto Server with MQTT Protocol. NodeMCU board is the subscriber and MyMQTT mobile application is acts as a subscriber to send the messages to NodeMCU through the centralized cloud.



NodeMCU

A NodeMCU is a development board with an inbuilt Wi-Fi module in it. It is a basic and cost-efficient board to carry out projects using the internet of things. The NodeMCU has an ESP8266 microcontroller unit in it. The operation of this microcontroller is controlled with the programs used in Arduino thus making it very easier to use and also to learn basic IoT projects. This board has an inbuilt 2.4GHz antenna to receive Wi-Fi functions. This board has a memory of 4mb to store the data acting as ROM and 64Kb of RAM. This board operates at 3.3 volts and it is mandatory to operate the board at this voltage and not more than that as increasing the input voltage in this board may damage few GPIO pins (general input-output pins).


Pin Configuration of NodeMCU

1. Vin: 3.3V can be provided at this pin as the supply to power on the board. This pin is used to power on the entire microcontroller.

2. GND: This pin is connected to the negative terminal of the battery.

3. RST: This pin resets the microcontroller and clears the memory.

4. EN: This pin is used to enable the operation of microcontroller.

5. 3V3: This pin provides 3V output and this can be used to power up some sensor units connected to the microcontroller.

6. SD1, CMD, SD0, CLK: These pins are used in SPI communication, that is it is used to transfer the signals between two microcontrollers, Rx and Tx modules with asynchronous transmission.

7. SD3, SD2: These pins can also function as asynchronous transmission or as GPIO pins.

8. RSV: These are two reserved pins used by the microcontroller and cannot be used in connecting any external circuits to it.

9. A0: This microcontroller only has one analog pin for Analog communication. This A0 pin is used in analog signal communication.

10. GPIO 1 – 16: This controller board has 16 input-output pins which be used as input or output pin based on the programming.

11. GP10 1, 3, 13, 15: This microcontroller has 2 UART communication pins, RX0, TX0 (GPIO 1 & GPIO 3) and RX1, TX1 (GPIO 13, GPIO15).

Schematic


Code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "WIFI-NAME";
const char* password = "WIFI-PASSWORD";
const char* mqtt_server = "test.mosquitto.org";
const char* device_id = "esp8266";
WiFiClient espClient;
PubSubClient client(espClient);
//======================================
// CALLBACK FUNCTION
//=======================================
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");Serial.print(topic);Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '1') {
    digitalWrite(D1, HIGH);
	} 
	else{
    digitalWrite(D1, LOW);  // Turn the LED off by making the voltage HIGH
	}
}
//======================================
// RECONNECT FUNCTION
//=======================================
void reconnect() {
 while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(device_id))
    { 
    Serial.println("connected");
    client.subscribe("esp8266/light/gemlabs");
    }                                                                              
  }
}
//======================================
// SETUP FUNCTION
//=======================================
void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());
  pinMode(D1, OUTPUT);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
//======================================
// LOOP FUNCTION
//=======================================
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close